/*
 * Copyright (C) 2010 TopCoder Inc., All Rights Reserved.
 */
package com.hercules.android.receiver.painter;

import java.util.Map;

import android.graphics.Canvas;
import android.graphics.Matrix;

import com.hercules.android.receiver.Receiver;
import com.hercules.android.receiver.ReceiverUtil;
import com.hercules.android.receiver.entities.XREObject;
import com.hercules.android.receiver.entities.XREView;

/**
 * <p>
 * This class is a painter class which will draw a XREView resource on a canvas.
 * </p>
 * 
 * @author TCSASSEMBER
 * @version 1.0
 */
public class XREViewPainter extends XREPainter {
    /**
     * Constructor with a <code>Receiver</code> arguments.
     * 
     * @param receiver
     *            the <code>Receiver</code> instance.
     * @throws IllegalArgumentException
     *             if receiver if null
     */
    public XREViewPainter(Receiver receiver) {
        super(receiver);
    }

    /**
     * This method will paint the resource on canvas.
     * 
     * @param view
     *            the resource to paint
     * @param canvas
     *            the canvas that the resource painted on
     * @throws IllegalArgumentException
     *             if arguments is null
     * @throws XREPainterException
     *             if any error occurs
     */
    public void paint(XREView view, Canvas canvas) throws XREPainterException {
        // check the arguments
        ReceiverUtil.checkNull("view", view);
        ReceiverUtil.checkNull("canvas", canvas);

        if (!view.getVisible() || !view.getPainting()) {
            return;
        }

        Matrix matrix = new Matrix();
        matrix.setValues(view.getMatrix());
        int sc = canvas.save();
        canvas.concat(matrix);

        int saveClip = 0;
        if (view.getClip()) {
            saveClip = canvas.save();
            canvas.clipRect(0, 0, view.getDimensions()[0], view.getDimensions()[1]);
        }

        if (view.getResource() > 0) {
            // paint resource
            XREObject object = receiver.getApplicationData().getElementsXREIdToElement().get(view.getResource());
            Map<String, XREPainter> objectPainters = receiver.getApplicationData().getObjectPainters();
            if (object != null) {
                if (!objectPainters.containsKey(object.getKlass())) {
                    throw new XREPainterException("Can't find painter for klass:" + object.getKlass());
                }
                objectPainters.get(object.getKlass()).paint(view, canvas);
            }
        }

        // paint children views
        for (XREView child : view.getChildren()) {
            int sc2 = canvas.save();
            this.paint(child, canvas);
            canvas.restoreToCount(sc2);
        }

        if (view.getClip()) {
            // restore canvas
            canvas.restoreToCount(saveClip);
        }
        canvas.restoreToCount(sc);
    }
}
